﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilLib
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static partial class Util
    {


        #region 自用

        //private const long UnixEpochTicks = 621355968000000000;
        //private const long UnixEpochSeconds = 62135596800;
        //private const long UnixEpochMilliseconds = 62135596800000;

        ///// <summary>
        ///// 获取该时间相对于1970-01-01 00:00:00的秒数
        ///// </summary>
        ///// <param name="dt"></param>
        ///// <returns></returns>
        //public static int UnixTimeToSeconds(this DateTime dt)
        //{
        //    if (dt.Kind == DateTimeKind.Utc)
        //    {
        //        return (int)((dt - UtilEnum.EpochDateTimeUtc).TotalSeconds);
        //    }
        //    else
        //    {
        //        return (int)((dt - UtilEnum.EpochDateTime).TotalSeconds);
        //    }
        //}
            
        ///// <summary>
        ///// 获取时间 本地时区
        ///// </summary>
        ///// <param name="seconds"></param>
        ///// <returns></returns>
        //public static DateTime UnixSecondsToTime(this int seconds)
        //{
        //    return new DateTime(TimeSpan.TicksPerSecond * seconds, DateTimeKind.Local);
        //}

        ///// <summary>
        ///// 获取时间 标准时区
        ///// </summary>
        ///// <param name="seconds"></param>
        ///// <returns></returns>
        //public static DateTime UnixSecondsToTimeUtc(this int seconds)
        //{
        //    return new DateTime(TimeSpan.TicksPerSecond * seconds, DateTimeKind.Utc);
        //}

        ///// <summary>
        ///// 获取该时间相对于1970-01-01 00:00:00的秒数
        ///// </summary>
        ///// <param name="dt"></param>
        ///// <returns></returns>
        //public static int UnixTimeToSeconds(this DateTime dt) => (int)(new TimeSpan(dt.Ticks - UnixEpochTicks).TotalSeconds);
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="seconds"></param>
        ///// <returns></returns>
        //public static DateTime UnixSecondsToTime(this int seconds) => UtilEnum.EpochDateTime.AddSeconds(seconds);

        #endregion

        #region 系统自带

        /// <summary>
        /// 
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public static DateTime FromUnixTimeSeconds(this long seconds) => DateTimeOffset.FromUnixTimeSeconds(seconds).DateTime;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="milliseconds"></param>
        /// <returns></returns>
        public static DateTime FromUnixTimeMilliseconds(this long milliseconds) => DateTimeOffset.FromUnixTimeMilliseconds(milliseconds).DateTime;
        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的秒数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int ToUnixTimeSeconds(this DateTime dt) => (int)(new DateTimeOffset(dt).ToUnixTimeSeconds());
        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的毫秒数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToUnixTimeMilliseconds(this DateTime dt) => new DateTimeOffset(dt).ToUnixTimeMilliseconds();
        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的微秒时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToUnixTimeMicroseconds(this DateTime dt) => new DateTimeOffset(dt).Ticks / 10;
        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的纳秒时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToUnixTimeNanoseconds(this DateTime dt) => new DateTimeOffset(dt).Ticks * 100 + Stopwatch.GetTimestamp() % 100;
        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的分钟数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double ToUnixTimeMinutes(this DateTime dt) => new DateTimeOffset(dt).Offset.TotalMinutes;
        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的小时数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double ToUnixTimeHours(this DateTime dt) => new DateTimeOffset(dt).Offset.TotalHours;
        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的天数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double ToUnixTimeDays(this DateTime dt) => new DateTimeOffset(dt).Offset.TotalDays;

        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的秒数
        /// </summary>
        /// <param name="ticks"></param>
        /// <returns></returns>
        public static int ToUnixTimeSeconds(this long ticks) => ticks > 0 ? (int)(new DateTimeOffset(new DateTime(ticks)).ToUnixTimeSeconds()) : 0;

        #endregion


    }
}
